//TESH.scrollpos=90
//TESH.alwaysfold=0

/**
 * 如果你想要用DZAPI，复制下面代码进触发器（注意是直接复制，不能include）
 * If you want the DZAPI execute already, copy the following code into your trigger.
 * Copy directly without #include.
 */

globals

    // 充足的触发队列
    integer Hlua_DzAPI_Tgr_count = 9
    trigger Hlua_DzAPI_Tgr_1
    trigger Hlua_DzAPI_Tgr_2
    trigger Hlua_DzAPI_Tgr_3
    trigger Hlua_DzAPI_Tgr_4
    trigger Hlua_DzAPI_Tgr_5
    trigger Hlua_DzAPI_Tgr_6
    trigger Hlua_DzAPI_Tgr_7
    trigger Hlua_DzAPI_Tgr_8
    trigger Hlua_DzAPI_Tgr_9

    boolean HLUA_DZAPI_FLAG = true
    hashtable hash_hlua_dzapi

    integer HLDK_COMMAND = 101
    integer HLDK_PLAYER = 102
    integer HLDK_KEY = 103
    integer HLDK_DATA = 104
    integer HLDK_RESULT = 120

endglobals

library zzzDZapi initializer Init

    public function callback takes nothing returns nothing
        local trigger tgr = GetTriggeringTrigger()
        local integer tid = GetHandleId(tgr)
        local string command = LoadStr(hash_hlua_dzapi, tid, HLDK_COMMAND)
        local player p = null
        local string ky = null
        local string data = null
        if (command == "IsRedVIP") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            if(DzAPI_Map_IsRedVIP(p))then
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "1")
            else
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "0")
            endif
        elseif (command == "IsBlueVIP") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            if(DzAPI_Map_IsBlueVIP(p))then
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "1")
            else
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "0")
            endif
        elseif (command == "GetPlayerServerValueSuccess") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            if(DzAPI_Map_GetServerValueErrorCode(p) == 0)then
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "1")
            else
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "0")
            endif
        elseif (command == "GetMapLevel") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, I2S(DzAPI_Map_GetMapLevel(p)))
        elseif (command == "HasMallItem") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            set ky = LoadStr(hash_hlua_dzapi, tid, HLDK_KEY)
            if(DzAPI_Map_HasMallItem(p, ky))then
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "1")
            else
                call SaveStr(hash_hlua_dzapi, tid, HLDK_RESULT, "0")
            endif
        elseif (command == "Stat_SetStat") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            set ky = LoadStr(hash_hlua_dzapi, tid, HLDK_KEY)
            set data = LoadStr(hash_hlua_dzapi, tid, HLDK_DATA)
            call DzAPI_Map_Stat_SetStat(p, ky, data)
        elseif (command == "SaveServerValue") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            set ky = LoadStr(hash_hlua_dzapi, tid, HLDK_KEY)
            set data = LoadStr(hash_hlua_dzapi, tid, HLDK_DATA)
            call DzAPI_Map_SaveServerValue(p, ky, data)
        elseif (command == "GetServerValue") then
            set p = LoadPlayerHandle(hash_hlua_dzapi, tid, HLDK_PLAYER)
            set ky = LoadStr(hash_hlua_dzapi, tid, HLDK_KEY)
            call SaveStr(hash_hlua_dzapi,  tid, HLDK_RESULT, DzAPI_Map_GetServerValue(p, ky))
        endif
        set tgr = null
        set command = null
        set p = null
        set ky = null
        set data = null
    endfunction

    private function Init takes nothing returns nothing
        set hash_hlua_dzapi = InitHashtable()
        set Hlua_DzAPI_Tgr_1 = CreateTrigger()
        set Hlua_DzAPI_Tgr_2 = CreateTrigger()
        set Hlua_DzAPI_Tgr_3 = CreateTrigger()
        set Hlua_DzAPI_Tgr_4 = CreateTrigger()
        set Hlua_DzAPI_Tgr_5 = CreateTrigger()
        set Hlua_DzAPI_Tgr_6 = CreateTrigger()
        set Hlua_DzAPI_Tgr_7 = CreateTrigger()
        set Hlua_DzAPI_Tgr_8 = CreateTrigger()
        set Hlua_DzAPI_Tgr_9 = CreateTrigger()
        call TriggerAddAction(Hlua_DzAPI_Tgr_1, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_2, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_3, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_4, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_5, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_6, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_7, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_8, function callback)
        call TriggerAddAction(Hlua_DzAPI_Tgr_9, function callback)
    endfunction

endlibrary